﻿using System;
using System.Composition;
using Unread.App.Services.Interfaces;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace Unread.App.Services
{
    [Export(typeof(IToastNotifier))]
    internal class ToastNotifier : IToastNotifier
    {
        public void Notify(int totalUnreadItems, TimeSpan delay)
        {
            var toast = CreateUnreadItemsTileNotification(totalUnreadItems, delay);

            ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
        }

        private ScheduledToastNotification CreateUnreadItemsTileNotification(int totalUnreadItems, TimeSpan delay)
        {
            var message = string.Format("{0} {1}", totalUnreadItems, App.Current.Resources["AppName"].ToString());
            var toast = CreateToastText01(message);
            var deliveryTime = DateTime.Now.Add(delay);

            return new ScheduledToastNotification(toast, deliveryTime)
            {
                Id = Guid.NewGuid().ToString().Substring(0, 15)
            };
        }

        private XmlDocument CreateToastText01(string bodyText)
        {
            var toastContent = string.Format(@"
                <toast>
                    <visual>
                        <binding template='ToastText01'>
                            <text id='1'>{0}</text>
                        </binding>
                    </visual>
                </toast>", bodyText);


            var toast = new XmlDocument();
            toast.LoadXml(toastContent);

            return toast;
        }
    }
}
